﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Collections.Specialized;
using System.Web;

namespace Yuanlin.Business
{
    public class ZiCaiSearchResult : YuanlinUserControl
    {
        #region 页面上的控件

        protected ValuedDropDownList provinceDropDownList;
        protected ValuedDropDownList cityDropDownList;
        protected ValuedDropDownList townDropDownList;
        protected ValuedDropDownList smallTownDropDownList;
        protected ValuedDropDownList zicaiTopLevelCategoryIDDropDownList;
        protected ValuedDropDownList zicaiSecondLevelCategoryIDDropDownList;
        protected ValuedDropDownList tradeTypeDropDownList;
        protected Button searchZiCaiButton;
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;

        #endregion

        #region Private Members

        private NameValueCollection formData;
        private string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);
        private int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);
        private int provinceId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProvinceId);
        private int cityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CityId);
        private int townId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.TownId);
        private int smallTownId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.SmallTownId);
        private int zicaiTopLevelCategoryID = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ZicaiTopLevelCategoryID);
        private int zicaiSecondLevelCategoryID = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ZicaiSecondLevelCategoryID);
        private string queryText = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.QueryText);
        private int level = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Level);
        private int area = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Area);
        private int defaultIndex = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.DefaultIndex);

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                Bind();
                BindZiCais();
            }
            searchZiCaiButton.Click += new EventHandler(searchZiCaiButton_Click);
            AjaxManager.Register(this, "Search");
        }
        //查询
        protected void searchZiCaiButton_Click(object sender, EventArgs e)
        {
            if (queryText == "-1")
            {
                queryText = "";
            }
            formData = HttpContext.Current.Request.Form;
            ReBindZiCais();
        }

        //绑定查询条件
        private void Bind()
        {
            //交易类型
            tradeTypeDropDownList.Items.Add(new ListItem(string.Format("<{0}>", ResourceManager.GetString("RequestSelect")), ""));
            tradeTypeDropDownList.Items.Add(new ListItem(ResourceManager.GetString("Sell"), "0"));
            tradeTypeDropDownList.Items.Add(new ListItem(ResourceManager.GetString("Buy"), "1"));
            tradeTypeDropDownList.Value = tradeType;

            #region 地区

            EntityList defaultCategories1 = new EntityList();
            EntityList defaultCategories2 = new EntityList();
            EntityList defaultCategories3 = new EntityList();
            EntityList defaultCategories4 = new EntityList();

            //默认值
            Area topCategory = new Area();
            topCategory.Title.Value = "<选择省>";
            topCategory.EntityId.Value = -1;

            Area topCategory1 = new Area();
            topCategory1.Title.Value = "<选择市>";
            topCategory1.EntityId.Value = -1;

            Area topCategory2 = new Area();
            topCategory2.Title.Value = "<选择县>";
            topCategory2.EntityId.Value = -1;

            Area topCategory3 = new Area();
            topCategory3.Title.Value = "<选择乡>";
            topCategory3.EntityId.Value = -1;

            //数据库中数据
            AreaListRequest request  = new AreaListRequest();
            request.UpAreaId = 0;
            defaultCategories1 = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));

            if (provinceId > 0)
            {
                request = new AreaListRequest();
                request.UpAreaId = provinceId;
                defaultCategories2 = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            }
            if (cityId > 0)
            {
                request = new AreaListRequest();
                request.UpAreaId = cityId;
                defaultCategories3 = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            }
            if (townId > 0)
            {
                request = new AreaListRequest();
                request.UpAreaId = townId;
                defaultCategories4 = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            }

            defaultCategories1.Insert(0, topCategory);
            defaultCategories2.Insert(0, topCategory1);
            defaultCategories3.Insert(0, topCategory2);
            defaultCategories4.Insert(0, topCategory3);

            //绑定控件
            this.provinceDropDownList.DataSource = defaultCategories1;
            this.provinceDropDownList.DataValueField = "EntityId";
            this.provinceDropDownList.DataTextField = "Title";
            this.provinceDropDownList.DataBind();
            this.provinceDropDownList.Value = provinceId.ToString();

            this.cityDropDownList.DataSource = defaultCategories2;
            this.cityDropDownList.DataValueField = "EntityId";
            this.cityDropDownList.DataTextField = "Title";
            this.cityDropDownList.DataBind();
            this.cityDropDownList.Value = cityId.ToString();

            this.townDropDownList.DataSource = defaultCategories3;
            this.townDropDownList.DataValueField = "EntityId";
            this.townDropDownList.DataTextField = "Title";
            this.townDropDownList.DataBind();
            this.townDropDownList.Value = townId.ToString();

            this.smallTownDropDownList.DataSource = defaultCategories4;
            this.smallTownDropDownList.DataValueField = "EntityId";
            this.smallTownDropDownList.DataTextField = "Title";
            this.smallTownDropDownList.DataBind();
            this.smallTownDropDownList.Value = smallTownId.ToString();

            #endregion

            #region 资材类别

            //默认值
            ZiCai topZiCai1 = new ZiCai();
            topZiCai1.Title.Value = string.Format("<{0}>", ResourceManager.GetString("RequestSelect"));
            topZiCai1.EntityId.Value = -1;

            //数据库中数据
            ZiCaiListRequest request1 = new ZiCaiListRequest();
            request1.UpZiCaiId = 0;
            EntityList firstLevelZiCaiList = BusinessManager.GetZiCaiList(RequestHelper.BuildGetZiCaiListRequest(request1));
            if (firstLevelZiCaiList.Count == 5)
            {
                firstLevelZiCaiList.RemoveAt(1);
            }
            EntityList secondLevelZiCaiList = new EntityList();
            zicaiTopLevelCategoryID = kind;
            if (zicaiTopLevelCategoryID > 0)
            {
                request1 = new ZiCaiListRequest();
                request1.UpZiCaiId = zicaiTopLevelCategoryID;
                secondLevelZiCaiList = BusinessManager.GetZiCaiList(RequestHelper.BuildGetZiCaiListRequest(request1));
            }

            firstLevelZiCaiList.Insert(0, topZiCai1);
            secondLevelZiCaiList.Insert(0, topZiCai1);

            //绑定控件
            this.zicaiTopLevelCategoryIDDropDownList.DataSource = firstLevelZiCaiList;
            this.zicaiTopLevelCategoryIDDropDownList.DataValueField = "EntityId";
            this.zicaiTopLevelCategoryIDDropDownList.DataTextField = "Title";
            this.zicaiTopLevelCategoryIDDropDownList.DataBind();
            this.zicaiTopLevelCategoryIDDropDownList.Value = zicaiTopLevelCategoryID.ToString();
            if (defaultIndex > 0)
            {
                zicaiTopLevelCategoryIDDropDownList.Enabled = false;
            }
            
            this.zicaiSecondLevelCategoryIDDropDownList.DataSource = secondLevelZiCaiList;
            this.zicaiSecondLevelCategoryIDDropDownList.DataValueField = "EntityId";
            this.zicaiSecondLevelCategoryIDDropDownList.DataTextField = "Title";
            this.zicaiSecondLevelCategoryIDDropDownList.DataBind();
            this.zicaiSecondLevelCategoryIDDropDownList.Value = zicaiSecondLevelCategoryID.ToString();

            #endregion
        }
        //显示数据
        private void BindZiCais()
        {
            EntityList ziCaiInfos = BusinessManager.GetZiCaiInfoList(RequestHelper.BuildGetZiCaiInfoListRequest(zicaiTopLevelCategoryID, zicaiSecondLevelCategoryID, System.Web.HttpUtility.HtmlEncode(queryText), level, area, System.Web.HttpUtility.HtmlEncode(tradeType), kind, pager.PageIndex, pager.PageSize));

            list.DataSource = ziCaiInfos;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(ziCaiInfos.TotalCount);
                currentPage.TotalRecords = ziCaiInfos.TotalCount;
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }
        //重新刷新页面
        private void ReBindZiCais()
        {
            //种类
            int zicaiTopLevelCategoryID = 0;
            if (!string.IsNullOrEmpty(formData[this.zicaiTopLevelCategoryIDDropDownList.UniqueID]))
            {
                zicaiTopLevelCategoryID = int.Parse(formData[this.zicaiTopLevelCategoryIDDropDownList.UniqueID]);
                kind = zicaiTopLevelCategoryID;
            }
            //类别
            int zicaiSecondLevelCategoryID = 0;
            if (!string.IsNullOrEmpty(formData[this.zicaiSecondLevelCategoryIDDropDownList.UniqueID]))
            {
                zicaiSecondLevelCategoryID = int.Parse(formData[this.zicaiSecondLevelCategoryIDDropDownList.UniqueID]);
            }
            //交易类型
            string tradeType = string.Empty;
            if (!string.IsNullOrEmpty(formData[this.tradeTypeDropDownList.UniqueID]))
            {
                tradeType = formData[this.tradeTypeDropDownList.UniqueID];
            }

            #region 地区

            int provinceID = 0,cityID = 0,townID = 0,smallTownID = 0,areaID = 0,level = 0;
            if (!string.IsNullOrEmpty(formData[provinceDropDownList.UniqueID]))
            {
                provinceID = int.Parse(formData[provinceDropDownList.UniqueID]);
            }
            if (!string.IsNullOrEmpty(formData[cityDropDownList.UniqueID]))
            {
                cityID = int.Parse(formData[cityDropDownList.UniqueID]);
            }
            if (!string.IsNullOrEmpty(formData[townDropDownList.UniqueID]))
            {
                townID = int.Parse(formData[townDropDownList.UniqueID]);
            }
            if (!string.IsNullOrEmpty(formData[smallTownDropDownList.UniqueID]))
            {
                smallTownID = int.Parse(formData[smallTownDropDownList.UniqueID]);
            }
            if (smallTownID > 0)
            {
                areaID = smallTownID;
                level = 4;
            }
            else if (townID > 0)
            {
                areaID = townID;
                level = 3;
            }
            else if (cityID > 0)
            {
                areaID = cityID;
                level = 2;
            }
            else if (provinceID > 0)
            {
                areaID = provinceID;
                level = 1;
            }

            #endregion
            
            HttpContext.Current.Response.Redirect(SiteUrls.Instance().GetZiCaiSearchResultUrl(areaID, level, zicaiTopLevelCategoryID, zicaiSecondLevelCategoryID, System.Web.HttpUtility.HtmlEncode(tradeType), System.Web.HttpUtility.HtmlEncode(queryText), provinceID, cityID, townID, smallTownID, kind,defaultIndex));
        }
        //地区
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = parentCategoryID;
            EntityList categories = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));

            if (categories != null && categories.Count > 0)
            {
                string categoryFormat = "{0}:{1}";
                string categoryString = string.Empty;
                foreach (Area category in categories)
                {
                    if (categoryString != string.Empty)
                    {
                        categoryString += ";";
                    }
                    categoryString += string.Format(categoryFormat, category.EntityId.Value.ToString(), category.Title.Value);
                }
                return categoryString;
            }

            return string.Empty;
        }
        //类别
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetZiCaiChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }
            ZiCaiListRequest request = new ZiCaiListRequest();
            request.UpZiCaiId = parentCategoryID;
            EntityList categories = BusinessManager.GetZiCaiList(RequestHelper.BuildGetZiCaiListRequest(request));
            if (categories != null && categories.Count > 0)
            {
                string categoryFormat = "{0}:{1}";
                string categoryString = string.Empty;
                foreach (ZiCai category in categories)
                {
                    if (categoryString != string.Empty)
                    {
                        categoryString += ";";
                    }
                    categoryString += string.Format(categoryFormat, category.EntityId.Value.ToString(), category.Title.Value);
                }
                return categoryString;
            }

            return string.Empty;
        }
    }
}